package com.example.automatic.abs_video;

import android.accessibilityservice.AccessibilityService;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.example.automatic.utils.LogUtils;

import java.util.List;

public class FKCCYAccessibilityService extends AccessibilityService {

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.e("TAG", "onServiceConnected=========================");
    }

    @Override
    public void onAccessibilityEvent(final AccessibilityEvent event) {
        AccessibilityNodeInfo accessibilityNodeInfo1 = getRootInActiveWindow();
        if (accessibilityNodeInfo1 != null) {
            LogUtils.e(event.getClassName() + "，" + accessibilityNodeInfo1.getClassName() + "，，，，" + event.toString());
        }

        if (event.getPackageName().equals("com.yztc.studio.plugin")) {
            LogUtils.e("《抹机王》");
            if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
                if (event.getClassName().equals("com.yztc.studio.plugin.module.main.activity.MainActivity")) {
                    //主页面
                } else if (event.getClassName().equals("com.yztc.studio.plugin.module.wipedev.basesetting.WipedevBaseSettingActivity")) {
                    //基础配置功能页面
                } else if (event.getClassName().equals("com.yztc.studio.plugin.module.wipedev.wipetask.WipeTaskActivity")) {
                    //清理运行页面
                }
            }
        }

        if (event.getPackageName().equals("com.kamitu.drawsth.standalone.free.android")) {
            LogUtils.e("《疯狂猜成语》");

            if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
                if (event.getClassName().equals("com.qsmy.busniess.b.b.a.b")) {
                    AccessibilityNodeInfo n8NodeInfo =
                            getAccessibilityNodeInfo("com.kamitu.drawsth.standalone.free.android:id/n8");
                    if (n8NodeInfo != null) {
                        n8NodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    }
                } else if (event.getClassName().equals("com.qsmy.common.view.widget.dialog.VisitorRewardDialog")) {
                    List<CharSequence> texts = event.getText();
                } else if (event.getClassName().equals("com.qsmy.common.view.widget.dialog.VisitorRewardResultDialog")) {

                }
            }
        }
    }

    @Override
    public void onInterrupt() {
        Log.e("TAG", "onInterrupt=========================");
    }


    /**
     * 返回指定操作控件
     */
    public AccessibilityNodeInfo getAccessibilityNodeInfo(String viewId, String viewText) {
        AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
        if (accessibilityNodeInfo == null) {
            return null;
        }
        List<AccessibilityNodeInfo> gridFunctionList = accessibilityNodeInfo
                .findAccessibilityNodeInfosByViewId(viewId);
        for (AccessibilityNodeInfo nodeInfo : gridFunctionList) {
            if (viewText == null) {
                return nodeInfo;
            } else if (nodeInfo.getText().equals(viewText)) {
                return nodeInfo;
            }
        }
        return null;
    }

    public AccessibilityNodeInfo getAccessibilityNodeInfoByText(String viewText) {
        AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
        if (accessibilityNodeInfo == null) {
            return null;
        }
        List<AccessibilityNodeInfo> gridFunctionList = accessibilityNodeInfo
                .findAccessibilityNodeInfosByText(viewText);
        for (AccessibilityNodeInfo nodeInfo : gridFunctionList) {
            if (nodeInfo.getText().equals(viewText)) {
                return nodeInfo;
            }
        }
        return null;
    }

    public AccessibilityNodeInfo getAccessibilityNodeInfo(String viewId) {
        return this.getAccessibilityNodeInfo(viewId, null);
    }


    private AccessibilityNodeInfo findByID(AccessibilityNodeInfo rootInfo, String text) {
        if (rootInfo.getChildCount() > 0) {
            for (int i = 0; i < rootInfo.getChildCount(); i++) {
                AccessibilityNodeInfo child = rootInfo.getChild(i);
                try {
                    List<AccessibilityNodeInfo> nodeInfoList = child.findAccessibilityNodeInfosByViewId(text);
                    if (nodeInfoList.size() > 0) {
                        return nodeInfoList.get(0);
                    }
                } catch (Exception e) {
                    return null;
                }
                findByID(child, text);//递归一直找一层层的全部遍历
            }
        }
        return null;
    }
}
